﻿Imports FinanceiroBLL
Imports FinanceiroDAL.ModuloConexao
Public Class PlanodeContasUC
    Dim objBLL As New PlanoContasBLL
    Private Sub TSBVoltar_Click(sender As System.Object, e As System.EventArgs) Handles TSBVoltar.Click
        statusfinanceiro = "N"
        Dispose()
    End Sub

    Private Sub TSBNovo_Click(sender As System.Object, e As System.EventArgs) Handles TSBNovo.Click
        Try
            Dim frm As New PlanoContasFRM
            frm.Op = "I"
            TSBNovo.BackColor = Color.Gainsboro
            frm.ShowDialog()
            CarregaGrid()
        Catch ex As Exception
        Finally
            TSBNovo.BackColor = Color.Transparent
        End Try
    End Sub

#Region "Carrega Grid"
    Sub CarregaGrid()
        DGVDados.DataSource = objBLL.buscar("", 4)
        DGVDados.Refresh()
    End Sub
#End Region
#Region "Load"
    Private Sub PlanodeContasUC_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        PLocalizar.Visible = False
        'Determinar o começo o filtro como descrição
        CBFiltro.SelectedIndex = 1
        CBFiltro.Focus()
        CarregaGrid()
    End Sub
#End Region

    Private Sub TSBAlterar_Click(sender As System.Object, e As System.EventArgs) Handles TSBAlterar.Click
        Dim indice As Integer = -1
        Try
            For Each linha As DataGridViewRow In DGVDados.SelectedRows
                If (Not linha.Cells("ccc_codigo") Is Nothing) Then
                    indice = linha.Index
                End If
            Next
            If (indice <> -1) Then
                PlanoContasFRM.codigo = Val(DGVDados.Rows(indice).Cells("ccc_codigo").Value)
                PlanoContasFRM.Op = "A"
                TSBAlterar.BackColor = Color.Gainsboro
                PlanoContasFRM.ShowDialog()
                CarregaGrid()
            Else
                MessageBox.Show("Selecione a Conta a ser alterada", "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            TSBAlterar.BackColor = Color.Transparent
        End Try
    End Sub
#Region "Excluir"
    Private Sub TSBExcluir_Click(sender As System.Object, e As System.EventArgs) Handles TSBExcluir.Click
        Try
            Dim cod As Integer = 0
            Dim indice As Integer = -1
            For Each linha As DataGridViewRow In DGVDados.SelectedRows
                If (Not linha.Cells("ccc_codigo") Is Nothing) Then
                    indice = linha.Index
                End If
            Next
            If (indice <> -1) Then
                TSBExcluir.BackColor = Color.Gainsboro

                If MessageBox.Show("Confirma exclusão da Conta " & DGVDados.Rows(indice).Cells("ccc_descricao").Value.ToString() & " ?", "", MessageBoxButtons.YesNo) = vbYes Then
                    cod = DGVDados.Rows(indice).Cells("ccc_codigo").Value.ToString()
                    If (objBLL.Excluir(cod)) Then
                        MsgBox("Exclusão efetuada com sucesso!", MsgBoxStyle.Information)
                        CarregaGrid()
                    Else
                        MsgBox("Conta não pode ser excluída", MsgBoxStyle.Exclamation)
                    End If
                Else
                    MessageBox.Show("Selecione a conta a ser excluida", "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Information)
                End If
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            TSBExcluir.BackColor = Color.Transparent
        End Try
    End Sub
#End Region
#Region "Localizar"
    Private Sub TSBLocalizar_Click(sender As System.Object, e As System.EventArgs) Handles TSBLocalizar.Click
        If PLocalizar.Visible = True Then
            PLocalizar.Visible = False
            TXTLocalizar.Text = ""
            TSBLocalizar.BackColor = Color.Transparent
        Else
            PLocalizar.Visible = True
            TXTLocalizar.Text = ""
            TSBLocalizar.BackColor = Color.Gainsboro
        End If
    End Sub
#End Region

    Private Sub CBFiltro_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles CBFiltro.KeyPress
        e.Handled = True
    End Sub

    Private Sub CBFiltro_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles CBFiltro.SelectedIndexChanged
        If CBFiltro.SelectedIndex = 2 Then
            CBStatus.Visible = True
            TXTLocalizar.Visible = False
            CBStatus.SelectedIndex = 0
        Else
            CBStatus.Visible = False
            TXTLocalizar.Visible = True
        End If
        TXTLocalizar.Text = ""
        CarregaGrid()
    End Sub

    Private Sub CBStatus_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles CBStatus.SelectedIndexChanged
        DGVDados.DataSource = objBLL.buscar(CBStatus.Text, CBFiltro.SelectedIndex)
    End Sub

    Private Sub TXTLocalizar_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TXTLocalizar.KeyPress
        If CBFiltro.SelectedIndex = 0 Then
            If Not Char.IsNumber(e.KeyChar) And Not e.KeyChar = vbBack Then
                e.Handled = True
            End If
        End If
    End Sub

    Private Sub TXTLocalizar_TextChanged(sender As System.Object, e As System.EventArgs) Handles TXTLocalizar.TextChanged
        Select Case CBFiltro.Text
            Case "Código"
                DGVDados.DataSource = objBLL.buscar(TXTLocalizar.Text, CBFiltro.SelectedIndex)
            Case "Descrição"
                DGVDados.DataSource = objBLL.buscar(TXTLocalizar.Text, CBFiltro.SelectedIndex)
        End Select
        If TXTLocalizar.Text = "" Then
            DGVDados.DataSource = objBLL.buscar("", 4)
        End If
    End Sub

#Region "Cor de fundo do datagridview"
    Private Sub DGVDados_RowPrePaint(sender As System.Object, e As System.Windows.Forms.DataGridViewRowPrePaintEventArgs) Handles DGVDados.RowPrePaint
        If DGVDados.Rows(e.RowIndex).Index Mod (2) = 1 Then
            DGVDados.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.Gainsboro
        End If
    End Sub
#End Region
#Region "Detalhes"
    Private Sub TSBDetalhes_Click(sender As System.Object, e As System.EventArgs) Handles TSBDetalhes.Click
        For Each linha As DataGridViewRow In DGVDados.SelectedRows
            If (Not linha.Cells("ccc_codigo") Is Nothing) Then
                objBLL = New PlanoContasBLL(Val(DGVDados.Rows(linha.Index).Cells("ccc_codigo").Value.ToString()))
            End If
        Next
        If (Not IsNothing(objBLL)) Then
            If (objBLL.Codigo > 0) Then gerarelatrio(objBLL.Descricao, "{Ofi_ClassCentroCusto.ccc_codigo}=" & objBLL.Codigo)
        End If
    End Sub
    Public Sub gerarelatrio(titulo As String, formula As String)
        Try
            Dim frmrecursos As New RelatoriosfinFRM
            Dim rpt As New PlanoContasREL

            Try
                rpt.SetDatabaseLogon(usuario, senha)
                rpt.SetParameterValue(0, titulo)
                rpt.RecordSelectionFormula = formula
                frmrecursos.CrystalReportViewer2.ShowGroupTreeButton = False
                frmrecursos.CrystalReportViewer2.ReportSource = rpt
                frmrecursos.ShowDialog()
            Catch ex As Exception
                MessageBox.Show(ex.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
            frmrecursos.Dispose()
            rpt = Nothing
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Finally

        End Try
    End Sub
#End Region
End Class
